package com.yanqu.road.entity.systemgroup.rank;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class UserRankModel {

    private LinkedList<UserRank> rankList = new LinkedList<>();

    private Comparator sort = new UserRankSort();

    private int rankNum = 20000;

    public UserRankModel() {
    }

    public UserRankModel(int rankNum) {
        this.rankNum = rankNum;
    }

    public void setRankList(LinkedList<UserRank> rankList) {
        synchronized (this) {
            this.rankList = new LinkedList<>(rankList);
            this.rankList.sort(sort);
            while (this.rankList.size() > rankNum){
                this.rankList.removeLast();
            }
        }
    }

    public int getMyRank(long userId) {
        int rank = -1;
        int index = 0;
        synchronized (this) {
            for(UserRank userRank : rankList){
                if(userRank.getUserId() == userId){
                    rank = index + 1;
                    break;
                }
                index++;
            }
        }
        if(rank > rankNum){
            rank = -1;
        }
        return rank;
    }

    public UserRank getMyRankInfo(long userId) {
        synchronized (this) {
            for(UserRank userRank: rankList){
                if(userRank.getUserId() == userId){
                    return userRank;
                }
            }
        }
        return null;
    }

    /**
     * 添加到排行
     */
    public void rankChange(UserRank userRank) {
        boolean isChange = false;
        synchronized (this) {
            int size = rankList.size();
            UserRank rank = getMyRankInfo(userRank.getUserId());
            if(null != rank){
                if(userRank.getUserBaseInfo() != null) {
                    rank.setUserBaseInfo(userRank.getUserBaseInfo());
                }
                rank.setLastUpdateTime(userRank.getLastUpdateTime());
                rank.setValue(userRank.getValue());
                isChange = true;
            }else {
                if (size < rankNum) {
                    rankList.add(userRank);
                    isChange = true;
                } else if (rankList.getLast().getValue().compareTo(userRank.getValue()) < 0) {
                    rankList.removeLast();
                    rankList.add(userRank);
                    isChange = true;
                }
            }
            if(isChange) {
                rankList.sort(sort);
            }
        }
    }

    public List<UserRank> getRankList(int topCount) {
        List<UserRank> list = new LinkedList<>();
        for (UserRank rank : new LinkedList<>(rankList)) {
            if (list.size() >= topCount) {
                break;
            }
            list.add(rank);
        }
        return list;
    }

    public LinkedList<UserRank> getRankList() {
        LinkedList<UserRank> tempList = new LinkedList<>();
        for (UserRank rank : new ArrayList<>(rankList)) {
            tempList.add(rank);
            if (tempList.size() >= rankNum) {
                break;
            }
        }
        return tempList;
    }
}
